const db = require("../config/config");
const jsonWebToken = require("jsonwebtoken");
const SECRET_KEY = "kite1874";
module.exports = {
  getProduct(req, res) {
    let mysql = `SELECT * FROM PRODUCT`;
    db.dbMysql(mysql, null, function (err, data) {
      for (let i = 0; i < data.length; i++) {
        if (data[i].productGrounding == 1) {
          data[i].productGrounding = true;
        } else {
          data[i].productGrounding = false;
        }
      }
      const data1 = data
        .map((item) => {
          if (item.state == 1) {
            return item;
          }
          return null;
        })
        .filter((item) => item !== null);
      const data2 = data1
        .map((item) => {
          if (item.productState == 0) {
            return item;
          }
          return null;
        })
        .filter((item) => item !== null);

      if (err) {
        return res.json({
          code: 500,
          msg: "获取商品失败，请稍后重试",
        });
      }
      res.json({
        code: 200,
        msg: "获取商品成功",
        data: data2,
      });
    });
  },
  getProduct1(req, res) {
    let mysql = `SELECT * FROM PRODUCT`;
    db.dbMysql(mysql, null, function (err, data) {
      const data1 = data.map((item) => {
        if (item.state == 0) {
          item.state = "待审核";
          return item;
        } else if (item.state == 1) {
          item.state = "已通过";
          return item;
        } else if (item.state == 2) {
          item.state = "未通过";
          return item;
        }
      });
      const data2 = data1
        .map((item) => {
          if (item.productState == 0) {
            return item;
          }
          return null;
        })
        .filter((item) => item !== null);
      if (err) {
        return res.json({
          code: 500,
          msg: "获取商品失败，请稍后重试",
        });
      }
      res.json({
        code: 200,
        msg: "获取商品成功",
        data: data2,
      });
    });
  },
  getProduct2(req, res) {
    let mysql = `SELECT * FROM PRODUCT`;
    db.dbMysql(mysql, null, function (err, data) {
     if(data){
      const data1 = data
      .map((item) => {
        if (item.productState == 1) {
          return item;
        }
        return null;
      })
      .filter((item) => item !== null);
    if (err) {
      return res.json({
        code: 500,
        msg: "获取商品失败，请稍后重试",
      });
    }
    res.json({
      code: 200,
      msg: "获取商品成功",
      data: data1,
    });
     }
    })
  },
  getClassify(req, res) {
    let mysql = `SELECT * FROM CLASSIFY`;
    db.dbMysql(mysql, null, function (err, data) {
      if (err) {
        return res.json({
          code: 500,
          msg: "获取分类失败，请稍后重试",
        });
      }
      res.json({
        code: 200,
        msg: "获取分类成功",
        data: data,
      });
    });
  },
  getProductBrand(req, res) {
    let mysql = `SELECT * FROM PRODUCT`;
    db.dbMysql(mysql, null, function (err, data) {
      let arr = [];
      for (let i = 0; i < data.length; i++) {
        arr.push(data[i].productBrand);
      }
      arr = Array.from(new Set(arr));
      if (err) {
        return res.json({
          code: 500,
          msg: "获取分类失败，请稍后重试",
        });
      }
      res.json({
        code: 200,
        msg: "获取分类成功",
        data: arr,
      });
    });
  },
  addProduct(req, res) {
    let productID = req.body.productID;
    let productUrl = req.body.productUrl;
    let productName = req.body.productName;
    let productPrice = req.body.productPrice;
    let productRule = req.body.productRule;
    let productColor = req.body.productColor;
    let productIDCard = req.body.productIDCard;
    let productNum = req.body.productNum;
    let productBrand = req.body.productBrand;
    let productPay = req.body.productPay;
    let productVideo = req.body.productVideo;
    let postage = req.body.postage;
    let productService = req.body.productService;
    let productClassify1 = req.body.productClassify1;
    let productClassify2 = req.body.productClassify2;
    let mysql = `INSERT INTO product (productID,productUrl,productName,productPrice,productRule,productColor,productIDCard,productNum,productBrand,productPay,productVideo,postage,productService,productClassify1,productClassify2) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);`;
    db.dbMysql(
      mysql,
      [
        productID,
        productUrl,
        productName,
        productPrice,
        productRule,
        productColor,
        productIDCard,
        productNum,
        productBrand,
        productPay,
        productVideo,
        postage,
        productService,
        productClassify1,
        productClassify2,
      ],
      function (err, data) {
        if (err) {
          return res.json({
            code: 500,
            msg: "添加商品失败，请稍后重试",
          });
        }
        res.json({
          code: 200,
          msg: "添加商品成功",
        });
      }
    );
  },
  changeProduct(req, res) {
    let productID = req.body.productID;
    let productUrl = req.body.productUrl;
    let productName = req.body.productName;
    let productPrice = req.body.productPrice;
    let productRule = req.body.productRule;
    let productColor = req.body.productColor;
    let productIDCard = req.body.productIDCard;
    let productNum = req.body.productNum;
    let productBrand = req.body.productBrand;
    let productPay = req.body.productPay;
    let productVideo = req.body.productVideo;
    let postage = req.body.postage;
    let productService = req.body.productService;
    let productClassify1 = req.body.productClassify1;
    let productClassify2 = req.body.productClassify2;
    let mysql = `update product set productUrl=? , productName =? , productPrice=? , productRule=? , productColor=? , productIDCard=? , productNum=? , productBrand=? , productPay=? , productVideo=? , postage=? , productService=? , productClassify1=? , productClassify2=? where productID=?`;
    db.dbMysql(
      mysql,
      [
        productUrl,
        productName,
        productPrice,
        productRule,
        productColor,
        productIDCard,
        productNum,
        productBrand,
        productPay,
        productVideo,
        postage,
        productService,
        productClassify1,
        productClassify2,
        productID,
      ],
      function (err, data) {
        if (err) {
          return res.json({
            code: 500,
            msg: "添加商品失败，请稍后重试",
          });
        }
        res.json({
          code: 200,
          msg: "添加商品成功",
        });
      }
    );
  },
  quiteProduct1(req, res) {
    let productName = req.body.productName;
    let productBrand = req.body.productBrand;
    let productClassify1 = req.body.productClassify1;
    let productClassify2 = req.body.productClassify2;
    let state = req.body.state;
    let productGrounding = req.body.productGrounding;
    console.log(req.body);
    let mysql = `select * from PRODUCT where productState=0 `;
    if (productName != "") {
      mysql = mysql + " and productName=% '" + productName + "%'";
    }
    if (productBrand != "请选择") {
      mysql = mysql + " and productBrand= '" + productBrand + "'";
    }
    if (productClassify1 != "请选择一级分类") {
      mysql = mysql + " and productClassify1= '" + productClassify1 + "'";
    }
    if (productClassify2 != "请选择二级分类") {
      mysql = mysql + " and productClassify2= '" + productClassify2 + "'";
    }
    if (state != "请选择"&&state != undefined) {
      mysql = mysql + " and state= '" + state + "'";
    }
    console.log(mysql);
    db.dbMysql(mysql, null, function (err, data) {
      const data1 = data.map((item) => {
        if (item.state == 0) {
          item.state = "待审核";
          return item;
        } else if (item.state == 1) {
          item.state = "已通过";
          return item;
        } else if (item.state == 2) {
          item.state = "未通过";
          return item;
        }
      });
      if (err) {
        return res.json({
          code: 500,
          msg: "获取商品失败，请稍后重试",
        });
      }
      res.json({
        code: 200,
        msg: "获取商品成功",
        data: data1,
      });
    });
  },
  quiteProduct2(req, res) {
    let productName = req.body.productName;
    let productBrand = req.body.productBrand;
    let productClassify1 = req.body.productClassify1;
    let productClassify2 = req.body.productClassify2;
    let state = req.body.state;
    let productGrounding = req.body.productGrounding;
    let mysql = `select * from PRODUCT where productState = 0 and state=1`;
    if (productName != "") {
      mysql = mysql + " and productName=% '" + productName + "%'";
    }
    if (productBrand != "请选择") {
      mysql = mysql + " and productBrand= '" + productBrand + "'";
    }
    if (productClassify1 != "请选择一级分类") {
      mysql = mysql + " and productClassify1= '" + productClassify1 + "'";
    }
    if (productClassify2 != "请选择二级分类") {
      mysql = mysql + " and productClassify2= '" + productClassify2 + "'";
    }
    if (productGrounding != "请选择") {
      mysql = mysql + " and productGrounding= '" + productGrounding + "'";
    }
    db.dbMysql(mysql, null, function (err, data) {
      if (err) {
        return res.json({
          code: 500,
          msg: "获取商品失败，请稍后重试",
        });
      }
      res.json({
        code: 200,
        msg: "获取商品成功",
        data: data,
      });
    });
  },
  changeProductGrounding(req, res) {
    let productID = req.body.productID;
    let state = req.body.state;
    console.log(req.body);
    let mysql = `update product set state=? where productID=?`;
    db.dbMysql(mysql, [state, productID], function (err, data) {
      console.log(mysql);
      if (err) {
        return res.json({
          code: 500,
          msg: "审核商品失败，请稍后重试",
        });
      }
      res.json({
        code: 200,
        msg: "审核商品成功",
      });
    });
  },
  changeProductGrounding1(req, res) {
    let productID = req.body.productID;
    let productGrounding = req.body.productGrounding;
    if (productGrounding == true) {
      productGrounding = 1;
    } else {
      productGrounding = 0;
    }
    let mysql = `update product set productGrounding=? where productID=?`;
    db.dbMysql(mysql, [productGrounding, productID], function (err, data) {
      if (err) {
        return res.json({
          code: 500,
          msg: "商品上架失败，请稍后重试",
        });
      }
      res.json({
        code: 200,
        msg: "商品上架成功",
      });
    });
  },
  changeProductState(req, res) {
    let productID = req.body.productID;
    console.log(req.body);
    let mysql = `update product set productState=1 where productID=?`;
    db.dbMysql(mysql, [productID], function (err, data) {
      console.log(mysql);
      if (err) {
        return res.json({
          code: 500,
          msg: "删除商品失败，请稍后重试",
        });
      }
      res.json({
        code: 200,
        msg: "删除商品成功",
      });
    });
  },
  changeProductState1(req, res) {
    for (let i = 0; i < req.body.length; i++) {
      let productID = req.body[i];
      let mysql = `update product set productState=1 where productID=?`;
      db.dbMysql(mysql, [productID], function (err, data) {
        console.log(mysql);
        if (err) {
          return res.json({
            code: 500,
            msg: "删除商品失败，请稍后重试",
          });
        }
        res.json({
          code: 200,
          msg: "删除商品成功",
        });
      });
    }
  },
  changeProductState2(req, res) {
    let productID = req.body.productID;
    console.log(req.body);
    let mysql = `update product set productState=0 where productID=?`;
    db.dbMysql(mysql, [productID], function (err, data) {
      console.log(mysql);
      if (err) {
        return res.json({
          code: 500,
          msg: "还原商品失败，请稍后重试",
        });
      }
      res.json({
        code: 200,
        msg: "还原商品成功",
      });
    });
  },
  changeProductState3(req, res) {
    for (let i = 0; i < req.body.length; i++) {
      let productID = req.body[i];
      let mysql = `update product set productState=0 where productID=?`;
      db.dbMysql(mysql, [productID], function (err, data) {
        console.log(mysql);
        if (err) {
          return res.json({
            code: 500,
            msg: "还原商品失败，请稍后重试",
          });
        }
        res.json({
          code: 200,
          msg: "还原商品成功",
        });
      });
    }
  },
  changeProductNum(req, res) {
    let productID = req.body.productID;
    let productNum = req.body.productNum;
    let mysql = `update product set productNum=? where productID=?`;
    db.dbMysql(mysql, [productNum, productID], function (err, data) {
      if (err) {
        return res.json({
          code: 500,
          msg: "修改商品库存失败，请稍后重试",
        });
      }
      res.json({
        code: 200,
        msg: "修改商品库存成功",
      });
    });
  },
  delProduct(req, res) {
    let productID = req.body.productID;
    console.log(req.body);
    let mysql = `DELETE FROM product WHERE productID = ?`;
    db.dbMysql(mysql, [productID], function (err, data) {
      console.log(mysql);
      if (err) {
        return res.json({
          code: 500,
          msg: "删除商品失败，请稍后重试",
        });
      }
      res.json({
        code: 200,
        msg: "删除商品成功",
      });
    });
  },
  delProduct2(req, res) {
    for (let i = 0; i < req.body.length; i++) {
      let productID = req.body[i].productID;
      let mysql = `DELETE FROM product WHERE productID = ?`;
      db.dbMysql(mysql, [productID], function (err, data) {
        console.log(mysql);
        if (err) {
          return res.json({
            code: 500,
            msg: "删除商品失败，请稍后重试",
          });
        }
        res.json({
          code: 200,
          msg: "删除商品成功",
        });
      });
    }
  },
};
